package com.icinfo.cloud.provider.punish.ucase.investigateevidence.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.icinfo.cloud.common.security.util.UserUtil;
import com.icinfo.cloud.provider.common.constant.Constant;
import com.icinfo.cloud.provider.common.enums.DocumentKindEnum;
import com.icinfo.cloud.provider.common.exception.BusinessException;
import com.icinfo.cloud.provider.common.utils.BaseUtil;
import com.icinfo.cloud.provider.punish.common.electricsent.dto.CaseTrialRecordQueryDto;
import com.icinfo.cloud.provider.punish.common.electricsent.dto.CaseTrialRecordSaveDto;
import com.icinfo.cloud.provider.punish.common.electricsent.vo.CaseTrialRecordDetailVo;
import com.icinfo.cloud.provider.punish.config.model.ConfigBusiParamDocumentInfo;
import com.icinfo.cloud.provider.punish.config.service.IConfigBusiParamDocumentInfoService;
import com.icinfo.cloud.provider.punish.ucase.casemain.dto.AppendixDto;
import com.icinfo.cloud.provider.punish.ucase.casemain.dto.CaseAttachmentInfoQueryDto;
import com.icinfo.cloud.provider.punish.ucase.casemain.mapper.CaseMainDetailInfoMapper;
import com.icinfo.cloud.provider.punish.ucase.casemain.mapper.CaseOfflineDocumentRecordMapper;
import com.icinfo.cloud.provider.punish.ucase.casemain.model.CaseAppendixRecord;
import com.icinfo.cloud.provider.punish.ucase.casemain.model.CaseMainDetailInfo;
import com.icinfo.cloud.provider.punish.ucase.casemain.model.CaseOfflineDocumentRecord;
import com.icinfo.cloud.provider.punish.ucase.casemain.service.ICaseAppendixRecordService;
import com.icinfo.cloud.provider.punish.ucase.casemain.service.ICaseMainDetailInfoService;
import com.icinfo.cloud.provider.punish.ucase.casemain.service.ICaseMainInfoService;
import com.icinfo.cloud.provider.punish.ucase.casemain.vo.CaseAttachmentInfoVo;
import com.icinfo.cloud.provider.punish.ucase.casemain.vo.CaseMainInfoVo;
import com.icinfo.cloud.provider.punish.ucase.investigateevidence.service.ICaseTrialRecordService;
import com.icinfo.cloud.workflow.common.annotation.WorkFlowRollAnnotation;
import com.icinfo.cloud.xzcf.provider.member.userinfo.dto.UserDTO;
import com.icinfo.framework.tools.utils.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * 描述: 案件审理记录 对应的Service接口实现类.<br>
 *
 * @author framework generator
 * @date 2022年02月18日
 */
@Service
public class CaseTrialRecordServiceImpl implements ICaseTrialRecordService {
    @Resource
    private ICaseMainInfoService caseMainInfoService;
    @Resource
    private CaseMainDetailInfoMapper caseMainDetailInfoMapper;
    @Resource
    private ICaseMainDetailInfoService caseMainDetailInfoService;
    @Resource
    private CaseOfflineDocumentRecordMapper caseOfflineDocumentRecordMapper;
    @Resource
    private IConfigBusiParamDocumentInfoService configBusiParamDocumentInfoService;
    @Resource
    private ICaseAppendixRecordService caseAppendixRecordService;

    @Override
    @Transactional(rollbackFor = {Error.class, Exception.class})
    @WorkFlowRollAnnotation
    public  Map<String, Object> saveCaseTrialRecord(CaseTrialRecordSaveDto saveDto) {
        UserDTO userInfo = UserUtil.getUserInfo();
        if (ObjectUtil.isEmpty(userInfo)) {
            throw new BusinessException("未登录或登录信息已失效");
        }
        if (ObjectUtils.isEmpty(saveDto.getAppendixList())){
            throw new BusinessException("审理记录附件不能为空");
        }
        String caseId=saveDto.getCaseId();
        CaseMainInfoVo caseMainInfo = caseMainInfoService.getCaseMainInfo(saveDto.getCaseId());
        //更新宽表的重大执法决定字段
        CaseMainDetailInfo updateDetailInfo=new CaseMainDetailInfo();
        updateDetailInfo.setCaseId(caseId);
        updateDetailInfo.setIsImportantLawDecision(saveDto.getIsImportantLawDecision());
        caseMainDetailInfoMapper.updateByPrimaryKeySelective(updateDetailInfo);
        ConfigBusiParamDocumentInfo configBusiParamDocumentInfo = configBusiParamDocumentInfoService.getDocumentInfoByDocumentKindCode(caseMainInfo.getBusiParamId(), DocumentKindEnum.DOC_KIND_DK2WFAJSLJL.getCode());
        String documentCatalogCode =configBusiParamDocumentInfo.getDocumentCatalogCode();
        //删除历史附件并保存附件
        caseAppendixRecordService.removeCaseAppendixRecordByCaseIdAndCatalogCode(caseId,documentCatalogCode);
        List<AppendixDto> appendixList = saveDto.getAppendixList();
        if (!ObjectUtils.isEmpty(appendixList)) {
            for (AppendixDto appendixDto : appendixList) {
                CaseAppendixRecord record = new CaseAppendixRecord();
                record.setCaseId(saveDto.getCaseId());
                record.setAppendixUrl(appendixDto.getAppendixUrl());
                record.setAppendixTypeId(appendixDto.getAppendixTypeId());
                record.setAppendixName(appendixDto.getAppendixName());
                record.setDocumentCatalogCode(documentCatalogCode);
                record.setId(BaseUtil.createUid());
                record.setCaseStageCode(caseMainInfo.getCaseCurrentStageCode());
                record.setCaseStageName(caseMainInfo.getCaseCurrentStageName());
                record.setCaseLinkCode(caseMainInfo.getCaseCurrentLinkCode());
                record.setCaseLinkName(caseMainInfo.getCaseCurrentLinkName());
                record.setPartyId(null);
                record.setCaseReasonId(null);
                caseAppendixRecordService.saveCaseAppendixRecord(record);
            }
        }
        // 更新上传线下文书表
        CaseOfflineDocumentRecord offlineDocumentRecord = new CaseOfflineDocumentRecord();
        offlineDocumentRecord.setCaseId(caseId);
        offlineDocumentRecord.setDocumentKindCode(DocumentKindEnum.DOC_KIND_DK2WFAJSLJL.getCode());
        offlineDocumentRecord.setDocumentKindName(DocumentKindEnum.DOC_KIND_DK2WFAJSLJL.getDes());
        offlineDocumentRecord.setCaseLinkCode(caseMainInfo.getCaseCurrentLinkCode());
        offlineDocumentRecord.setCaseLinkName(caseMainInfo.getCaseCurrentLinkName());
        offlineDocumentRecord.setCaseStageCode(caseMainInfo.getCaseCurrentStageCode());
        offlineDocumentRecord.setCaseStageName(caseMainInfo.getCaseCurrentStageName());
        offlineDocumentRecord.setDocumentType(configBusiParamDocumentInfo.getDocumentType());
        offlineDocumentRecord.setDocumentTittle(DocumentKindEnum.DOC_KIND_DK2WFAJSLJL.getDes());
        offlineDocumentRecord.setCreateUserId(userInfo.getUserId().toString());
        offlineDocumentRecord.setCreateUserName(userInfo.getRealName());
        offlineDocumentRecord.setStatus(Constant.STRING_1);
        offlineDocumentRecord.setDocumentUrl(appendixList.get(0).getAppendixUrl());
        offlineDocumentRecord.setDocumentCatalogCode(configBusiParamDocumentInfo.getDocumentCatalogCode());
        int num =0;
        if (ObjectUtil.isNotEmpty(saveDto.getDocumentId())) {
            offlineDocumentRecord.setId(saveDto.getDocumentId());
            num  = caseOfflineDocumentRecordMapper.updateByPrimaryKeySelective(offlineDocumentRecord);
        } else {
            offlineDocumentRecord.setId(StringUtils.uuid());
            offlineDocumentRecord.setCreateTime(LocalDateTime.now());
            num=  caseOfflineDocumentRecordMapper.insertSelective(offlineDocumentRecord);
        }

        Map<String, Object> resultMap = new HashMap<>();
        if (num < 1) {
            resultMap.put("success", "false");
            resultMap.put("msg", "保存失败！");
            return resultMap;
        }
        resultMap.put("documentCatalogCode", documentCatalogCode);
        resultMap.put("workSubId", "");
        resultMap.put("closeSignal", "CLOSE-TRIGGER-DOCCREATE");
        resultMap.put("workId", caseId);
        resultMap.put("success", "true");
        return  resultMap;
    }

    @Override
    public CaseTrialRecordDetailVo getCaseTrialRecord(CaseTrialRecordQueryDto queryDto) {
        String caseId=queryDto.getCaseId();
        CaseMainInfoVo caseMainInfo = caseMainInfoService.getCaseMainInfo(caseId);
        CaseTrialRecordDetailVo detailVo=new CaseTrialRecordDetailVo();
        //是否重大执法决定
        CaseMainDetailInfo caseMainDetailInfo = caseMainDetailInfoService.getCaseMainDetailByCaseId(caseId);
        detailVo.setIsImportantLawDecision(caseMainDetailInfo.getIsImportantLawDecision());
        ConfigBusiParamDocumentInfo configBusiParamDocumentInfo = configBusiParamDocumentInfoService.getDocumentInfoByDocumentKindCode(caseMainInfo.getBusiParamId(), DocumentKindEnum.DOC_KIND_DK2WFAJSLJL.getCode());
        String documentCatalogCode=configBusiParamDocumentInfo.getDocumentCatalogCode();
        if (ObjectUtil.isNotEmpty(documentCatalogCode)){
            //查询保存的附件
            CaseAttachmentInfoQueryDto queryAttachment = new CaseAttachmentInfoQueryDto();
            queryAttachment.setCaseId(caseId);
            queryAttachment.setDocumentCatalogCode(documentCatalogCode);
            List<CaseAttachmentInfoVo> caseAttachmentList = caseAppendixRecordService.getCaseAttachmentInfoList(queryAttachment);
            detailVo.setAppendixList(caseAttachmentList);
        }
        detailVo.setCaseId(caseId);
        return detailVo;
    }
}
